use actix_web::{HttpServer, App, middleware};
use actix_files::Files;

mod app;
mod view;
mod api;

use app::AppExt;

fn main() -> Result<(), failure::Error> {
    let clap_app = clap::App::new("月神论坛服务端程序")
        .version("0.1.0")
        .author("__JM_Joy__ <918734043@qq.com>")
        .about("开源免费的论坛服务端程序，使用强大的Rust语言开发")
        .arg(clap::Arg::with_name("host")
            .short("h")
            .long("host")
            .value_name("HOST")
            .help("Which host the service to listen.")
            .takes_value(true))
        .arg(clap::Arg::with_name("port")
            .short("p")
            .long("port")
            .value_name("PORT")
            .help("Which port the service to listen.")
            .takes_value(true))
        .arg(clap::Arg::with_name("rust-log")
            .long("rust-log")
            .value_name("LOG")
            .help("Set RUST_LOG to log to console.")
            .takes_value(true));

    let matches = clap_app.get_matches();

    let host = matches.value_of("host").unwrap_or("127.0.0.1");
    let port = matches.value_of("port").unwrap_or("8080");
    let rust_log = matches.value_of("rust-log").unwrap_or("info");

    std::env::set_var("RUST_LOG", rust_log);
    env_logger::init();

    HttpServer::new(||
        App::new()
            .data(app::AppState {})
            .wrap(middleware::Logger::default())
            .config_routes()
            .service(Files::new("/static", "./static/"))
    ).bind(&format!("{}:{}", host, port))?.run()?;

    Ok(())
}
